package cloud.daodao.storm.oauth2.repository;

import cloud.daodao.storm.oauth2.model.Role;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author DaoDao
 */
@Repository
public interface RoleRepository extends JpaRepository<Role, Long>, JpaSpecificationExecutor<Role> {

    /**
     * 查询用户角色
     *
     * @param userId Long
     * @return List
     */
    @Query("select r from User u" +
            " left join UserRole as ur on u.id = ur.userId" +
            " left join Role as r on ur.roleId = r.id" +
            " where u.id = ?1" +
            " and ur.valid = true" +
            " and r.valid = true")
    List<Role> findAllByUserId(Long userId);

}
